package com.example.spb_rabbitmq2.services;

import com.example.spb_rabbitmq2.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: climber zheng
 * @Desc:
 * @create: 2023-10-20 21:26
 **/
@Service
@Slf4j
public class MessageService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendInSimpleModel(Object msg) {
        log.info("发送消息：" + msg);
        rabbitTemplate.convertAndSend(RabbitMQConfig.SIMPLEQUEUE, msg);
    }

    public void sendInRouterModel(Object msg, String routeKey) {
        log.info("发送"+routeKey+"消息：" + msg);
        rabbitTemplate.convertAndSend(RabbitMQConfig.DIRECTEXCHANGE, routeKey, msg);
    }

    public void sendToFanoutExchange(Object msg) {
        log.info("发送消息：" + msg);
        rabbitTemplate.convertAndSend(RabbitMQConfig.FANOUTEXCHANGE, "", msg);
    }

    public void sendToTopic(String routeKey, String msg) {
        log.info("发送消息(被01队列接受)：" + msg);
        rabbitTemplate.convertAndSend(RabbitMQConfig.TopicEXCHANGE, routeKey, msg);
    }

    //    接收 简单模式 的消息
    @RabbitListener(queues = RabbitMQConfig.SIMPLEQUEUE)
    public void receiveSimple(Object msg) {
        log.info(RabbitMQConfig.SIMPLEQUEUE + " 接收消息：" + msg);
    }

    //    接收 路由模式 的消息
    @RabbitListener(queues = RabbitMQConfig.DIRECTQUEUE02)
    public void receiveFromRouter2(Object msg) {
        log.info(RabbitMQConfig.DIRECTQUEUE02 + " 接收消息：" + msg);
    }

    @RabbitListener(queues = RabbitMQConfig.DIRECTQUEUE01)
    public void receiveFromRouter1(Object msg) {
        log.info(RabbitMQConfig.DIRECTQUEUE01 + " 接收消息：" + msg);
    }


    //接收 订阅模式 的消息
    @RabbitListener(queues = RabbitMQConfig.FANOUTQUEUE01)
    public void receiveFromqueue_fanout01(Object msg) {
        log.info(RabbitMQConfig.FANOUTQUEUE01+" 接收消息：" + msg);
    }

    @RabbitListener(queues = RabbitMQConfig.FANOUTQUEUE02)
    public void receiveFromqueue_fanout02(Object msg) {
        log.info(RabbitMQConfig.FANOUTQUEUE02+" 接收消息：" + msg);
    }

    // 接收 通配符 模式
    @RabbitListener(queues = RabbitMQConfig.QUEUETopic01)
    public void receiveFromQueue_topic01(Object msg) {
        log.info(RabbitMQConfig.QUEUETopic01+" 接收消息：" + msg);
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUETopic02)
    public void receiveFromQueue_topic02(Object msg) {
        log.info(RabbitMQConfig.QUEUETopic02+" 接收消息：" + msg);
    }


}
